package cn.iocoder.yudao.module.system.controller.admin.mini.orderInfo.logisModul.service.logisticsmodul;

import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.module.system.controller.admin.mini.orderInfo.logisModul.controller.admin.logisticsmodul.vo.*;
import cn.iocoder.yudao.module.system.controller.admin.mini.orderInfo.logisModul.convert.logisticsmodul.LogisticsModulConvert;
import cn.iocoder.yudao.module.system.controller.admin.mini.orderInfo.logisModul.dal.dataobject.logisticsmodul.LogisticsModulDO;
import cn.iocoder.yudao.module.system.controller.admin.mini.orderInfo.logisModul.dal.mysql.logisticsmodul.LogisticsModulMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 运费模版 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class LogisticsModulServiceImpl implements LogisticsModulService {

    @Resource
    private LogisticsModulMapper logisticsModulMapper;

    @Override
    public Integer createLogisticsModul(LogisticsModulCreateReqVO createReqVO) {
        // 插入
        LogisticsModulDO logisticsModul = LogisticsModulConvert.INSTANCE.convert(createReqVO);
        for (LogisticsModulVo address : logisticsModul.getAddress()) {
            if (address.getFirst()==null)
                throw new ServiceException(500,"模版首重不能为空");
            if (address.getFirstPrice()==null)
                throw new ServiceException(500,"模版首重价格不能为空");
            if (address.getAfterPrice()==null)
                throw new ServiceException(500,"模版续重价格不能为空");
        }
        logisticsModulMapper.insert(logisticsModul);
        // 返回
        return logisticsModul.getId();
    }

    @Override
    public void updateLogisticsModul(LogisticsModulUpdateReqVO updateReqVO) {
        // 校验存在
        validateLogisticsModulExists(updateReqVO.getId());
        // 更新
        LogisticsModulDO updateObj = LogisticsModulConvert.INSTANCE.convert(updateReqVO);
        for (LogisticsModulVo address : updateObj.getAddress()) {
            if (address.getFirst()==null)
                throw new ServiceException(500,"模版首重不能为空");
            if (address.getFirstPrice()==null)
                throw new ServiceException(500,"模版首重价格不能为空");
            if (address.getAfterPrice()==null)
                throw new ServiceException(500,"模版续重价格不能为空");
        }
        logisticsModulMapper.updateById(updateObj);
    }

    @Override
    public void deleteLogisticsModul(Integer id) {
        // 校验存在
        validateLogisticsModulExists(id);
        // 删除
        logisticsModulMapper.deleteById(id);
    }

    private void validateLogisticsModulExists(Integer id) {
        if (logisticsModulMapper.selectById(id) == null) {
            throw exception(500);
        }
    }

    @Override
    public LogisticsModulDO getLogisticsModul(Integer id) {
        return logisticsModulMapper.selectById(id);
    }

    @Override
    public List<LogisticsModulDO> getLogisticsModulList(Collection<Integer> ids) {
        return logisticsModulMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<LogisticsModulDO> getLogisticsModulPage(LogisticsModulPageReqVO pageReqVO) {
        return logisticsModulMapper.selectPage(pageReqVO);
    }

    @Override
    public List<LogisticsModulDO> getLogisticsModulList(LogisticsModulExportReqVO exportReqVO) {
        return logisticsModulMapper.selectList(exportReqVO);
    }

}
